import { defineStore } from 'pinia'
import {getSysMenus} from '@/api/user/menu.js'
import router from '@/router'

export const useMenuStore = defineStore('menu', {
    // 定义状态
    state: () => ({
         menuList: [], 
         routerList: [],
         tabList: [{title: '首页', path: '/index'}],
         activeTab: '/index',
         userInfo: []
    }),
    // 获取数据的方法
    getters: {
        Array: (state) => state.menuList,
        Array: (state) => state.routerList,
        Array: (state) => state.tabList,
        String: (state) => state.activeTab,
        Array: (state) => state.userInfo,
    },
    // 修改数据的方法
    actions: {
        setMenuList(data) {
            this.menuList = data
        },
        // routerList存储到pinia中
        setRouterList(data) {
            data.forEach(item => {
                // 定义数据结构
                let routerInfo = {
                    name: item.name,
                    path: item.path,
                    meta: {
                        title: item.name
                    },
                    // 定义组件
                    // component: () => import(/*@vite-ignore */`../views${item.path}.vue`),
                    component: `../views${item.path}.vue`,
                }
                // 将路由信息添加到数组中
                this.routerList.push(routerInfo)
                // 设置子菜单
                let childrenList = item.children
                childrenList.forEach(child => {
                    let childInfo = {
                        name: child.name,
                        path: child.path,
                        meta: {title: child.name},
                        // 定义组件
                        // component: () => import(/*@vite-ignore */`../views${child.path}.vue`)
                        component: `../views${child.path}.vue`,
                    }
                    this.routerList.push(childInfo)
                })
            })
            console.log('setRouterList data = ', data)
            if (this.routerList.length > 0) {

                // 去重
                // this.routerList = [new Set(this.routerList.map(JSON.stringify))].map(JSON.parse)
                this.routerList = this.routerList.reduce((uniqueList, currentObj) => {
                    const res = uniqueList.some((obj) => obj.path === currentObj.path)
                    if (!res) {
                        uniqueList.push(currentObj)
                    }
                    return uniqueList
                }, [])
            }
            console.log('setRouterList = ', this.routerList)
        },
        getRouterList() {
            
            // this.routerList.forEach(router => {
            //     const path = router.component
            //     console.log('path = ', path)
            //     router.component = () => import(/*@vite-ignore */ path)
            // })

            return this.routerList.map(router => {
                const path = router.component
                // console.log('router = ', router)
                const newRouter = {
                   ...router,
                   component: () => import(/*@vite-ignore */ `${path}`)
                }
                // console.log('newRouter = ', newRouter)
                return newRouter
             })
        },
        // 发送请求查询用户菜单，并生成动态路由结构
        generateRouter() {
            return new Promise((resolve, reject) => {
                // 查询用户菜单
                getSysMenus().then(resp => {
                    if (resp.data.code == 200) {
                        this.setRouterList(resp.data.data)
                        resolve()
                    }
                    reject()
                })
            })
        },
        // 设置tag 的tabList
        setTabList(data) {
            this.tabList.push(data)
        },
        // 删除tabList
        removeTabList(name) {
            this.tabList = this.tabList.filter(item => {
                // console.log('item path = ', item.path + '\nnamne = ',name)
                if (item.path == name) {
                    // console.log('return false')
                    return false
                }
                // console.log('return true')
                return true
            })
            // console.log('remove tablist = ', this.tabList)
        },
        // 设置activeTab
        setActiveTab(name) {
            this.activeTab = name
        },
        // 设置userInfo
        setUserInfo(data) {
            this.userInfo = data
        }
    },

    // 开始pinia数据持久化
    // persist: {
    //     enabled: true,
    //     storage: localStorage,
    //     key: 'useMenu',
    //     path: ['menuList', 'routerList'],
    // },  
    persist: true,
})